const assert = require('assert');
const BasicLayout = require('../BasicController');

module.exports = class extends BasicLayout {
  constructor(ctx) {
    super(ctx);
    assert(think.isFunction(this.model), 'this.model must be a function');
    this.MODEL = this.ctx.state.ApiInfo.model;
    this.params = {
      model: this.MODEL,
      data: this.post()
    };
  }
  async addnoticeAction() {
    const params = this.params;
    params.data.founder = this.ctx.state.UserInfo.num;
    const data = await this.model(params.model).addnoticeAction(params);
    return this.success(data);
  }
  async selectAction() {
    const params = this.params;
    const data = await this.model('notice_richtext').where(params.data).find();
    return this.success(data);
  }
  async editnoticeAction() {
    const params = this.params;
    const data = await this.model(params.model).editnoticeAction(params);
    return this.success(data);
  }
  async saveAction() {
    const params = this.params;
    const data = await this.model(params.model).saveAction(params);
    return this.success(data);
  }
  async releaseAction() {
    const params = this.params;
    let databack = '';
    this.model(params.model).releaseAction(params);
    const datauseful = await this.model(params.data.type).where({id: params.data.id}).find();
    if (params.data.type === 'notice') {
      databack = (datauseful.select_stu ? datauseful.select_stu.split(',') : ['ios', 'web']).map(val => {
        return {
          notice_id: datauseful.id,
          user_id: val,
          deadline: datauseful.deadline
        };
      });
    } else {
      const stu = await this.model('student_info').field('id').select();
      const tea = await this.model('teacher_info').field('id').select();
      const arr = [...stu, ...tea];
      databack = arr.map(ele => {
        return {
          notice_id: datauseful.id,
          user_id: ele.id,
          deadline: datauseful.deadline
        };
      });
    }
    databack.forEach(element => {
      const id = think.uuid('v4');
      element.id = id;
      element.status = 1;
      const nowdate = new Date();
      const s1 = nowdate.getTime();
      const nowdate2 = new Date(element.deadline);
      const s2 = nowdate2.getTime();
      const total = s2 - s1;
      if (total < 0) {
        element.status = 1;
      } else {
        element.status = 0;
      }
    });
    const ischange = await this.model('user_notice').addAction(databack);
    return this.success(ischange);
  }
};
/**      element.status = 1;
 * @api {POST} /notice/notice/addnotice /addnotice
 * @apiName addnotice
 * @apiGroup notice
 * @apiParam {String} msg_header 消息头
 * @apiParam {String} select_stu 选中学生id
 * @apiParam {String} deadline 截止有效日期
 * @apiParam {String} richtext 富文本
 * @apiParam {String} type notice/announcement:通知还是公告
 * @apiParam {number} status 状态--新增不需要传，默认状态为０：草稿
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": {
      id:****************8,
    }
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {get} Request-Example:
                 {
                   msg_header:'dd',
                   select_stu:'1,2',
                   select_tea:'1,2,3',
                   deadline:'2016.08.15',
                  }
 */
/**
 * @api {POST} /notice/notice/editnotice /editnotice
 * @apiName editnotice
 * @apiGroup notice
 * @apiParam {String} id 通知条ID
 * @apiParam {String} msg_header 消息头
 * @apiParam {String} select_stu 选中学生id
 * @apiParam {String} deadline 截止有效日期
 * @apiParam {String} richtext 富文本
 * @apiParam {String} type notice/announcement:通知还是公告
 * @apiParam {number} status 状态--新增不需要传，默认状态为０：草稿
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": {
      id:****************8,
    }
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {get} Request-Example:
                 {
                   msg_header:'dd',
                   select_stu:'1,2',
                   select_tea:'1,2,3',
                   deadline:'2016.08.15',
                  }
 */
/**
 * @api {POST} /notice/notice/del /del
 * @apiName del
 * @apiGroup notice
 * @apiParam {String} id 通知条ID
 * @apiParam {String} type notice/announcement:通知还是公告
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 7
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                  id:*************,
                  }
 */
/**
 * @api {GET} /notice/notice/get /get
 * @apiName get
 * @apiGroup notice
 * @apiParam {String} id 通知ID
 * @apiParam {String} type notice/announcement:通知还是公告
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 7
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {get} Request-Example:
                 {
                   id:'*********',
                  }
 */
/**
 * @api {POST} /notice/notice/select /select
 * @apiName select
 * @apiGroup notice
 * @apiParam {String} [id] 通知条ID
 * @apiParam {String} [richText_id] 富文本ID
 * @apiParam {String} type notice/announcement:通知还是公告
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": []
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                  id:*************,
                  }
 */
/**
 * @api {POST} /notice/notice/listpage /listpage(分页,传空返全部，带查询)
 * @apiName listpage
 * @apiGroup notice
 * @apiParam {String} [id] 通知条ID
 * @apiParam {String} type notice/announcement:通知还是公告
 * @apiParam {String} [deadline] 截止日期
 * @apiParam {String} [createtime] 创建时间
 * @apiParam {number} [status] 通知条状态
 * @apiParam {String} [founder] 发布者
 * @apiParam {String} page 页数
 * @apiParam {String} pageSzie 数据条数
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data":  {
              "pageSize": 10,
        "current": 1,
        "pages": 1,
        "total": 2,
        data: [
            {
                "id": "e39383e0-5b21-42f9-8bc4-587c0f2f2b76",
                "msg_header": "D",
                "select_stu": "1,2",
                "select_tea": "1,2,3",
                "createtime": "2018-08-11 14:35:30",
                "deadline": "2013-02-12 00:00:00",
                "status": "1"
            }
          ]
        ]
      }
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {post} Request-Example:
                 {
                  page:1,
                  pageSize:10,
                  status:1,
                  }
 */
/**
 * @api {POST} /notice/notice/save /save(保存,存为草稿)
 * @apiName save
 * @apiGroup notice
 * @apiParam {String} id 通知ID
 * @apiParam {String} type notice/announcement:通知还是公告
 * @apiParam {String} [msg_header] 消息头
 * @apiParam {String} [select_stu] 选中学生id
 * @apiParam {String} [select_tea] 选中教师id
 * @apiParam {String} [deadline] 截止有效日期
 *
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 7
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {get} Request-Example:
                 {
                   id:'*********',
                   msg_header:'dd',
                   select_stu:'1,2',
                   select_tea:'1,2,3',
                   deadline:'2016.08.15',
                   status:'1',
                  }
 */
/**
 * @api {POST} /notice/notice/release /release(发布,提交)
 * @apiName release
 * @apiGroup notice
 * @apiParam {String} id 通知ID
 * @apiParam {String} type notice/announcement:通知还是公告
 * @apiParam {String} [msg_header] 消息头
 * @apiParam {String} [select_stu] 选中学生id
 * @apiParam {String} [select_tea] 选中教师id
 * @apiParam {String} [deadline] 截止有效日期
 * @apiSuccessExample 请求成功demo:
 *     HTTP/1.1 200 OK
 *     {
 *       {
    "errno": 0,
    "errmsg": "ok",
    "data": 7
}
 *     }
 *
 *
 * @apiErrorExample 请求失败返回demo:
 *     HTTP/1.1 404 Not Found
 *     {
 *           "errno": 404,
          "errmsg": "请求路径错误",
          "data": 7
 *     }
 * @apiParamExample {get} Request-Example:
                 {
                   id:'*********',
                   msg_header:'dd',
                   select_stu:'1,2',
                   select_tea:'1,2,3',
                   deadline:'2016.08.15',
                   status:'1',
                  }
 */
